home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / HexEdit 1.21 / ~Project / Source / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-03  |  9.9 KB  |  432 lines  |  [TEXT/CWIE]

  1. /************************************************************************************
  2.  * Main.c
  3.  *
  4.  * from HexEdit, a simple hex editor
  5.  * copyright 1993, Jim Bumgardner
  6.  *
  7.  * Revision History is in History.note
  8.  ************************************************************************************/
  9. #include "HexEdit.h"
  10. #include "AppleEvents.h"
  11. #include <Traps.h>
  12.  
  13.  
  14. void     MyInitMacintosh(void);
  15. void    MyInitMultifinder(void);
  16. void     InitAppleEvents(void);
  17. void    CheckEnvironment(void);
  18. void     MyHandleEvent(void);
  19. void     MyDoEvent(EventRecord *theEvent);
  20. void    IdleObjects(EventRecord *er);
  21.  
  22. Boolean    gWNEImplemented,gQuitFlag,gSys7Flag,gColorQDFlag;
  23.  
  24. //LR --- These are used to setup new UPP stuff ---
  25.  
  26. AEEventHandlerUPP AEHandlerUPP;
  27. extern ControlActionUPP trackActionUPP;
  28. extern DlgHookUPP myGetFileUPP;
  29.  
  30. extern pascal void MyScrollAction(ControlHandle theControl, short thePart);
  31. extern pascal int SourceDLOGHook(short item, DialogPtr theDialog);
  32.  
  33. // Main Entry Point
  34.  
  35. void main( void )    //LR :fix warnings
  36. {
  37.     // Standard Mac Initialization
  38.     MyInitMacintosh();
  39.  
  40.     // Check if Multifinder (WaitNextEvent) is implemented
  41.     MyInitMultifinder();
  42.  
  43.     // Check if System 7
  44.     CheckEnvironment();
  45.  
  46.     // Init Apple Events
  47.     InitAppleEvents();
  48.  
  49.     // Set up the menu bar
  50.     MySetUpMenus();
  51.  
  52.     InitializeEditor();
  53.  
  54.     if (!gSys7Flag)
  55.         AskEditWindow();
  56.  
  57.     // main event loop
  58.     while (!gQuitFlag)            // Till the End of Time...
  59.         MyHandleEvent();        // Get an Event, do something about it
  60.  
  61.     CleanupEditor();
  62. }
  63.  
  64. // Standard Macintosh Initialization
  65.  
  66. void MyInitMacintosh(void)
  67. {
  68.     MaxApplZone();
  69.     
  70.     InitGraf(&qd.thePort);    //LR :qd.
  71.     InitFonts();
  72.     FlushEvents(everyEvent, 0);
  73.     InitWindows();
  74.     InitMenus();
  75.     TEInit();
  76.     InitDialogs(0L);
  77.     InitCursor();
  78. }
  79.  
  80. // Check if WaitNextEvent (Multifinder) is implemented on this Macintosh
  81.  
  82. void MyInitMultifinder(void)
  83. {
  84.     gWNEImplemented = (NGetTrapAddress(_WaitNextEvent, ToolTrap) != 
  85.                        NGetTrapAddress(_Unimplemented,ToolTrap));
  86. }
  87.  
  88. // The Main Event Dispatcher - this routine should be called repeatedly
  89.  
  90. void MyHandleEvent(void)
  91.  
  92. {
  93.     EventRecord    theEvent;
  94.     Boolean        ok;
  95.  
  96.     // If the more modern WaitNextEvent is implemented, use it 
  97.  
  98.     if (gWNEImplemented)
  99.         // We don't have to call SystemTask because WaitNextEvent calls it for us
  100.         // Get the next event
  101.         ok = WaitNextEvent(everyEvent,&theEvent,0L,NULL);
  102.  
  103.     else {
  104.         // we are running in (Single) Finder under system 6 or less
  105.         // Give Desk Accessories some processing time
  106.         SystemTask ();
  107.  
  108.         // Get the next event
  109.         ok = GetNextEvent (everyEvent, &theEvent);
  110.     }
  111.  
  112.     if (IsDialogEvent(&theEvent)) {
  113.         DoModelessDialogEvent(&theEvent);
  114.     }
  115.     else if (ok) {
  116.             // Handle the Event
  117.             MyDoEvent(&theEvent);
  118.     }
  119.     else {
  120.         // Nothing happened, kick back...
  121.         IdleObjects(&theEvent);
  122.     }
  123. }
  124.  
  125.  
  126. void MyDoEvent(EventRecord *theEvent)
  127. {
  128.     short         windowCode;
  129.     WindowPtr    theWindow;
  130.  
  131.  
  132.     switch (theEvent->what) {
  133.     //
  134.     // Was the mouse button pressed?
  135.     case mouseDown:
  136.         // Find out where the mouse went down
  137.         windowCode = FindWindow (theEvent->where, &theWindow);
  138.  
  139.           switch (windowCode) {
  140.         case inSysWindow:     // Desk Accessory?
  141.             SystemClick (theEvent, theWindow);
  142.             break;
  143.             
  144.         case inMenuBar:        // Menu Bar?
  145.               MyAdjustMenus();
  146.             MyHandleMenu(MenuSelect(theEvent->where));
  147.             break;
  148.  
  149.         default:            // Cursor was inside our window
  150.             // If the window isn't in the front
  151.             if (theWindow != FrontWindow()) {
  152.                 // Make it so...
  153.                 SelectWindow(theWindow);
  154.                 MyAdjustMenus();
  155.             }
  156.             else {
  157.                 // Window is already in the front, handle the click
  158.                 switch (windowCode) {
  159.  
  160.                 case inContent:        // Content area?
  161.                     if (((WindowPeek) theWindow)->refCon == MyWindowID)
  162.                         ((ObjectWindowPtr) theWindow)->HandleClick(theWindow, theEvent->where, theEvent);
  163.                     break;
  164.  
  165.                 case inDrag:        // Dragbar?
  166.                     {
  167.                         Rect    dragRect;
  168.                         dragRect = qd.screenBits.bounds;    //LR :qd.
  169.                         // Handle the dragging of the window
  170.                         DragWindow(theWindow, theEvent->where, &dragRect);
  171.                         if (!((ObjectWindowPtr) theWindow)->floating)
  172.                             SelectWindow(theWindow);
  173.                     }
  174.                     break;
  175.  
  176.                  case inGoAway:                        // close box?
  177.                       if (TrackGoAway(theWindow, theEvent->where)) {
  178.                         // If mouse is released inside the close box
  179.                         // Hide or close the window
  180.                           if (((WindowPeek) theWindow)->refCon == MyWindowID)
  181.                             CloseEditWindow(theWindow);
  182.                         else if (theWindow == gSearchWin) {
  183.                             DisposeDialog(gSearchWin);
  184.                             gSearchWin = NULL;
  185.                         }
  186.                         MyAdjustMenus();
  187.                     }
  188.                       break;
  189.  
  190.                 case inGrow:                        // Grow box?
  191.                     {
  192.                         long    growResult;
  193.                         Rect    growRect;
  194.  
  195.                         SelectWindow(theWindow);
  196.  
  197.                         SetRect(&growRect,MaxWindowWidth+SBarSize-1,64,
  198.                                 MaxWindowWidth+SBarSize-1,gMaxHeight);
  199.  
  200.                         // Handle the mouse tracking for the resizing
  201.                         growResult = GrowWindow(theWindow,theEvent->where,&growRect);
  202.  
  203.                         // Change the size of the window
  204.                         SizeWindow(theWindow,LoWord(growResult),HiWord(growResult),true);
  205.  
  206.                         AdjustScrollBars(theWindow, true);
  207.                         DrawPage((EditWindowPtr) theWindow);
  208.  
  209.                         // Redraw the window
  210.                         SetPort(theWindow);
  211.                           InvalRect(&theWindow->portRect);
  212.                     }
  213.                     break;
  214.                 case inZoomIn:
  215.                 case inZoomOut:
  216.                     if (TrackBox(theWindow, theEvent->where, windowCode)) {
  217.                         SetPort(theWindow);
  218.                         EraseRect(&theWindow->portRect);
  219.                         ZoomWindow(theWindow, windowCode, true);
  220.                         AdjustScrollBars(theWindow, true);
  221.                         DrawPage((EditWindowPtr) theWindow);
  222.                         // Redraw the window
  223.                         SetPort(theWindow);
  224.                           InvalRect(&theWindow->portRect);
  225.                     }
  226.                 }
  227.             }
  228.             break;
  229.         }
  230.         break;
  231.         
  232.     // Was a key pressed?
  233.     case keyDown: 
  234.     case autoKey:
  235.         // Was the cmd-key being held down?  If so, process menu bar short cuts.
  236.         if ((theEvent->modifiers & cmdKey) != 0) {
  237.           MyAdjustMenus();
  238.           MyHandleMenu(MenuKey((char) (theEvent->message & charCodeMask)));
  239.         }
  240.         else {
  241.             theWindow = FrontWindow();
  242.             if (((WindowPeek) theWindow)->refCon == MyWindowID &&
  243.                 ((ObjectWindowPtr) theWindow)->ProcessKey != NULL)
  244.                 ((ObjectWindowPtr) theWindow)->ProcessKey(theWindow, theEvent);
  245.         }
  246.         break;
  247.  
  248.     // Does a window need to be redrawn?
  249.     case updateEvt:
  250.         theWindow = (WindowPtr) theEvent->message;
  251.         if (((WindowPeek) theWindow)->refCon == MyWindowID)
  252.             ((ObjectWindowPtr) theWindow)->Update(theWindow);
  253.         break;
  254.  
  255.     // Has a window been activated or deactivated?
  256.     case activateEvt:
  257.         theWindow = (WindowPtr) theEvent->message;
  258.  
  259.         // Force it to be redrawn
  260.         if (((WindowPeek) theWindow)->refCon == MyWindowID)
  261.             ((ObjectWindowPtr) theWindow)->Activate(theWindow,(theEvent->modifiers & activeFlag) > 0);
  262.  
  263.         break;
  264.     case osEvt:
  265.         // Force it to be redrawn
  266.         switch (theEvent->message >> 24) {
  267.         case suspendResumeMessage:
  268.             theWindow = FrontWindow();
  269.             if (theWindow && ((WindowPeek) theWindow)->refCon == MyWindowID)
  270.                 ((ObjectWindowPtr) theWindow)->Activate(theWindow,(theEvent->message & resumeFlag) > 0);
  271.             break;
  272.         }
  273.         break;        
  274.     case kHighLevelEvent:
  275.         if (gSys7Flag)
  276.             AEProcessAppleEvent(theEvent);
  277.         break;
  278.     }
  279. }        
  280.  
  281. // Do Idle Time Processing
  282.  
  283. void IdleObjects(EventRecord *er)
  284. {
  285.     WindowPeek    theWin;
  286.     theWin = (WindowPeek) FrontWindow();
  287.     while (theWin) {
  288.         if (theWin->refCon == MyWindowID &&
  289.             ((ObjectWindowPtr) theWin)->Idle)
  290.             ((ObjectWindowPtr) theWin)->Idle((WindowPtr) theWin, er);
  291.         theWin = theWin->nextWindow;
  292.     }
  293. }
  294.  
  295.  
  296.  
  297. static Boolean GotRequiredParams(AppleEvent *theEvent)
  298. {
  299.    DescType returnedType;
  300.    Size     actualSize;
  301.    OSErr    err;
  302.    err = AEGetAttributePtr ( theEvent, keyMissedKeywordAttr, 
  303.                         typeWildCard, &returnedType, NULL, 0, 
  304.                         &actualSize);
  305.    
  306.    return err == errAEDescNotFound;
  307.    
  308.  }    /* CAppleEvent::GotRequiredParams */
  309.  
  310.  
  311. static void DoOpenEvent(AppleEvent *theEvent)
  312. {
  313.     Handle        docList = NULL;
  314.     long        i;    //LR, numDocs;
  315.     FSSpec        myFSS;
  316. //LR    DescType    eventID;
  317.     AEDescList    theList;
  318.     AEKeyword    aeKeyword=keyDirectObject;
  319.     long        itemCount;
  320.     DescType    actualType;
  321.     Size        actualSize;
  322.     OSErr        oe;
  323.  
  324.     if ((oe = AEGetParamDesc( theEvent, keyDirectObject, typeAEList, &theList)) != noErr) {
  325.         DebugStr("\pAEGetParamDesc");
  326.         return;
  327.     }
  328.  
  329.  
  330.     if (!GotRequiredParams(theEvent)) {
  331.         DebugStr("\pGotRequiredParams");
  332.         return;
  333.     }
  334.  
  335.     if ((oe = AECountItems( &theList, &itemCount)) != noErr) {
  336.         DebugStr("\pAECountItems");
  337.         return;
  338.     }
  339.  
  340.  
  341.     for (i = 1; i <= itemCount; i++)
  342.     {
  343.         oe = AEGetNthPtr( &theList, i, typeFSS, &aeKeyword, &actualType,
  344.                         (Ptr) &myFSS, sizeof( FSSpec), &actualSize);
  345.  
  346.         if (oe == noErr) {
  347.             OpenEditWindow(&myFSS);
  348.         }
  349.     }
  350.     AEDisposeDesc(&theList);
  351.     // event was handled successfully
  352. }
  353.  
  354. static pascal OSErr AppleEventHandler(AppleEvent *theEvent,AppleEvent *reply, long refCon)
  355. {
  356. #pragma unused (reply,refCon)    //LR
  357.  
  358. //LR    OSErr        err;
  359.     DescType    actualType;
  360.     Size        actualSize;
  361.     DescType    eventClass, eventID;
  362.     OSErr        oe;
  363.  
  364.     if ((oe = AEGetAttributePtr( (AppleEvent*) theEvent, keyEventClassAttr,
  365.                     typeType, &actualType, (Ptr) &eventClass, 
  366.                     sizeof(eventClass), &actualSize)) != noErr)
  367.             return oe;
  368.                             
  369.     
  370.     if ((oe = AEGetAttributePtr(  (AppleEvent*) theEvent, keyEventIDAttr,
  371.                     typeType, &actualType, (Ptr) &eventID, 
  372.                     sizeof(eventID), &actualSize)) != noErr)
  373.             return oe;
  374.                                     
  375.     if (eventClass == kCoreEventClass)
  376.     {
  377.         switch (eventID)
  378.         {
  379.         case kAEOpenApplication:
  380.             if (GotRequiredParams(theEvent))
  381.             {
  382.                 // gGopher->DoCommand( cmdNew);
  383.                 // anAppleEvent->SetErrorResult( noErr);
  384.                 AskEditWindow();
  385.             }
  386.             break;
  387.                 
  388.         case kAEOpenDocuments:
  389.             DoOpenEvent( theEvent);
  390.             break;
  391.                 
  392.         case kAEPrintDocuments:
  393.             break;
  394.             
  395.         case kAEQuitApplication:
  396.             if (GotRequiredParams(theEvent))
  397.             {
  398.                 gQuitFlag = true;
  399.             }
  400.             break;
  401.         }        
  402.     }
  403.         
  404.     return noErr;
  405. }
  406.  
  407. //LR : fixed to conform to Universal Headers & work on PPC
  408.  
  409. void InitAppleEvents(void)
  410. {
  411.     if (gSys7Flag)
  412.     {
  413.         AEHandlerUPP = NewAEEventHandlerProc( AppleEventHandler );
  414.         AEInstallEventHandler( kCoreEventClass, typeWildCard, AEHandlerUPP, 0, FALSE );
  415.  
  416.         trackActionUPP = NewControlActionProc( MyScrollAction );
  417.         myGetFileUPP = NewDlgHookProc( SourceDLOGHook );
  418.     }
  419. }
  420.  
  421. void CheckEnvironment(void)
  422. {
  423.     SysEnvRec    sEnv;
  424.     OSErr        oe;
  425.  
  426.     oe = SysEnvirons(1,&sEnv);
  427.  
  428.     gSys7Flag = sEnv.systemVersion >= 0x0700;
  429.     gColorQDFlag = sEnv.hasColorQD;
  430. }
  431.  
  432. /* end Evtlab.c */